<?php
namespace repositories;
use Doctrine\ORM\EntityRepository;
use DoctrineExtensions\Paginate\Paginate;

class IM_Model_repo
	extends EntityRepository{
		
	public function getModels($s_gender, $s_category, $i_limitation){
		$s_query = 'SELECT a FROM entities\IM_Model a WHERE a.gender= ?1 AND a.'.$s_category .'=?2 AND a.published = ?3 ORDER BY a.name ASC';
		$rs = $this->getEntityManager()->createQuery($s_query);
		$rs->setParameter(1, $s_gender);
		$rs->setParameter(2,'x');
		$rs->setParameter(3,'x');
		if($i_limitation != null){
			$result= $rs->setFirstResult($i_limitation)->setMaxResults(18)->getResult();
		}
		
		//comment this if you want to enable checking
		$result = $rs->getResult();
		/*
		if(count($result) > 0){
			$result = $rs->getResult();
		}else{
			$s_query = 'SELECT a FROM entities\IM_Model a WHERE a.gender= ?1 AND a.'.$s_category .'=?2 AND a.published = ?3 ORDER BY a.name ASC';
			$rs = $this->getEntityManager()->createQuery($s_query);
			$rs->setParameter(1, $s_gender);
			$rs->setParameter(2,'x');
			$rs->setParameter(3,'x');
			$result = $rs->getResult();
		}
		*/
		return $result;
		
		/*
		$o_qb = $this->getEntityManager()->createQueryBuilder();
		$o_qb->select('a');
		$o_qb->from('entities\IM_Model', 'a');
		$o_qb->where('a.gender =?', $s_gender);
		$o_qb->andWhere('a.'.$s_category .'="x"');
		$dql = $o_qb->getDQL();
		
		$d_startDate = date('Y-m-d H:i:s', $d_start);
		$d_endDate = date('Y-m-d H:i:s', $d_end);
		$dql = 'SELECT a FROM Article a WHERE a.createDate BETWEEN ?1 AND ?2';
		$rs = $this->getEntityManager()->createQuery($dql);
		$rs->setParameter(1, $d_startDate);
		$rs->setParameter(2, $d_endDate);
		return $rs->getResult();
		*/
	}		
		
	public function getAll(){
		$s_query = 'SELECT a FROM entities\IM_Model a';
		$rs = $this->getEntityManager()->createQuery($s_query);
		$result = $rs->getResult();
		return $result;
	}
	
	public function getModelsByPage($i_offSet = 0, $i_maxResults = 10, $i_order=2, $s_dir = 'asc', $i_sEcho, $s_searchString, $s_catString)
	{
		$arr_columns = array('a.name', 'a.fullName', 'a.gender','a.updateDate', 'a.published', 'a.coverPhotoId');
		
		$o_qb = $this->getEntityManager()->createQueryBuilder();
		$o_qb->select('a');
		$o_qb->from('entities\IM_Model', 'a');
		switch($s_catString){
			case 'mainboard':
				//$o_qb->andWhere($o_qb->expr()->eq('a.mainBoard', 'x'));
				$o_qb->where($o_qb->expr()->eq('a.mainBoard', $o_qb->expr()->literal('x')));
				break;
				
			case 'development':
				$o_qb->where($o_qb->expr()->eq('a.development', $o_qb->expr()->literal('x')));
				break;
				
			case 'intown':
				$o_qb->where($o_qb->expr()->eq('a.inTown', $o_qb->expr()->literal('x')));
				break;
		}
		
		if($s_searchString != ''){
			foreach($arr_columns as $s_colName){
				$o_qb->orWhere($o_qb->expr()->like($s_colName, $o_qb->expr()->literal('%'.$s_searchString.'%')));
			}
			/*
			$o_qb->orWhere($o_qb->expr()->like($arr_columns[0], $o_qb->expr()->literal('%'.$s_searchString.'%')));
			$o_qb->orWhere($o_qb->expr()->like($arr_columns[1], $o_qb->expr()->literal('%'.$s_searchString.'%')));
			$o_qb->orWhere($o_qb->expr()->like($arr_columns[2], $o_qb->expr()->literal('%'.$s_searchString.'%')));
			$o_qb->orWhere($o_qb->expr()->like($arr_columns[3], $o_qb->expr()->literal('%'.$s_searchString.'%')));
			$o_qb->orWhere($o_qb->expr()->like($arr_columns[4], $o_qb->expr()->literal('%'.$s_searchString.'%')));
			*/
		}
		
		
		//$o_qb->andWhere('a.')
		//all actions are after 3
		if($i_order < 5){
			$o_qb->orderBy($arr_columns[$i_order], $s_dir);
		}
		
		$dql = $o_qb->getDQL();
		try{
			//set the offsett and the limit
			$rs = $this->getEntityManager()->createQuery($dql);
			$rs->setFirstResult($i_offSet)->setMaxResults($i_maxResults)->getResult(); // Step 2
		}catch(Exception $e){
			die($e);
		}
		$res = $rs->getResult();
		
		$arr_page = array(
			"sEcho"=> $i_sEcho,
			"iTotalRecords"=> $this->getTotModelCount(),
		 	"iTotalDisplayRecords"=> $this->getCurrModelCount($dql),
		 	"aaData" => $this->convertToPaginationFormat($res)
		);
		
		return $arr_page;
	}
	
	public function getCurrModelCount($q_currQuery)
	{
		$rs = $this->getEntityManager()->createQuery($q_currQuery);
		$count = Paginate::getTotalQueryResults($rs); // Step 1
		
		return $count;
	}
	
	private function getTotModelCount()
	{
		$dql = 'SELECT a.id FROM entities\IM_Model a';
		$rs = $this->getEntityManager()->createQuery($dql);
		$count = Paginate::getTotalQueryResults($rs); // Step 1
		
		return $count;
	}
	
	private function convertToPaginationFormat($arr_o_models)
	{
		$arr_appConf = Claycode_configuration::getInstance()->getAppConfByIdent('AdminUpdateModel');
		$arr_delAppConf = Claycode_configuration::getInstance()->getAppConfByIdent('AdminDeleteModel');
		$arr_modelPicsAppConf = Claycode_configuration::getInstance()->getAppConfByIdent('AdminManageModelPictures');
		$arr_modelCoverPhotoEdit = Claycode_configuration::getInstance()->getAppConfByIdent('AdminManageModelCoverPicture');
		$s_updateModelAlias = $arr_appConf['controller_class_alias'];
		
		$arr_delAppConfAlias = $arr_delAppConf['controller_class_alias'];
		$arr_updateModelPicsAlias = $arr_modelPicsAppConf['controller_class_alias'];
		$s_modelCoverPhotoEditClassAlias = $arr_modelCoverPhotoEdit['controller_class_alias'];
		
		$arr_rows = array();
		if(!empty($arr_o_models)){
			foreach($arr_o_models as $o_model){
				$i_modelId = $o_model->getId();
				$s_updateModelUrl = fw_util_getAbsoluteUrl() . $s_updateModelAlias . '/updateModel/ident/' .  EncryptionHandler::get()->doEncrypt($i_modelId);
				$s_deleteModelUrl = fw_util_getAbsoluteUrl() . $arr_delAppConfAlias . '/deleteModel/ident/' .  EncryptionHandler::get()->doEncrypt($i_modelId);
				$s_editModelPicturesUrl = fw_util_getAbsoluteUrl() . $arr_updateModelPicsAlias. '/setModelToEdit/ident/' .  EncryptionHandler::get()->doEncrypt($i_modelId);
				$s_editCoverPhotoUrl = fw_util_getAbsoluteUrl() . $s_modelCoverPhotoEditClassAlias . '/' . $arr_modelCoverPhotoEdit['action'] . '/ident/' . EncryptionHandler::get()->doEncrypt($i_modelId);
				
				if($o_model->getPublished() == 'x'){
					$s_modelPublished = 'yes';
				}else{
					$s_modelPublished = 'no';
				}
				$arr_rows[] = array(
					$o_model->getName(),
					$o_model->getFullName(),
					$o_model->getGender(),
					$o_model->getUpdateDate()->format('M d, Y H:m:s'),
					$s_modelPublished,
					'<a href="' . $s_editModelPicturesUrl . '">change/view</a>',
					'<a href="' . $s_updateModelUrl . '">change/view</a>',
					'<a href="' . $s_deleteModelUrl . '" class="del">remove</a>',
				);
			}
		}
		
		return $arr_rows;
	}
}